package cn.iocoder.yudao.module.question.service.bank;

import cn.hutool.core.collection.CollectionUtil;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;

import java.util.*;
import cn.iocoder.yudao.module.question.controller.admin.bank.vo.*;
import cn.iocoder.yudao.module.question.dal.dataobject.bank.BankDO;
import cn.iocoder.yudao.framework.common.pojo.PageResult;

import cn.iocoder.yudao.module.question.convert.bank.BankConvert;
import cn.iocoder.yudao.module.question.dal.mysql.bank.BankMapper;

import static cn.iocoder.yudao.framework.common.exception.util.ServiceExceptionUtil.exception;
import static cn.iocoder.yudao.module.question.enums.ErrorCodeConstants.*;

/**
 * 题库信息 Service 实现类
 *
 * @author 芋道源码
 */
@Service
@Validated
public class BankServiceImpl implements BankService {

    @Resource
    private BankMapper bankMapper;

    @Override
    public Long createBank(BankCreateReqVO createReqVO) {
        // 插入
        BankDO bank = BankConvert.INSTANCE.convert(createReqVO);
        bankMapper.insert(bank);
        // 返回
        return bank.getId();
    }

    @Override
    public void updateBank(BankUpdateReqVO updateReqVO) {
        // 校验存在
        validateBankExists(updateReqVO.getId());
        // 更新
        BankDO updateObj = BankConvert.INSTANCE.convert(updateReqVO);
        bankMapper.updateById(updateObj);
    }

    @Override
    public void deleteBank(Long id) {
        // 校验存在
        validateBankExists(id);
        // 删除
        bankMapper.deleteById(id);
    }

    private void validateBankExists(Long id) {
        if (bankMapper.selectById(id) == null) {
            throw exception(BANK_NOT_EXISTS);
        }
    }

    @Override
    public BankDO getBank(Long id) {
        return bankMapper.selectById(id);
    }

    @Override
    public List<BankDO> getBankList(Collection<Long> ids) {
        return CollectionUtil.isEmpty(ids)? bankMapper.selectList(): bankMapper.selectBatchIds(ids);
    }

    @Override
    public PageResult<BankDO> getBankPage(BankPageReqVO pageReqVO) {
        return bankMapper.selectPage(pageReqVO);
    }

    @Override
    public List<BankDO> getBankList(BankExportReqVO exportReqVO) {
        return bankMapper.selectList(exportReqVO);
    }

}
